<?php

/**
 * position actions.
 *
 * @package    virtualstore
 * @subpackage position
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class positionActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->positions = Doctrine::getTable('Position')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->position = Doctrine::getTable('Position')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->position);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PositionForm();
//     $et = Doctrine_Core::getTable('Employee');
//      $query = $et->getInactiveEmployeesQuery();
//      $options = array('model' => $this->form->getRelatedModelName('Employee'),
//          'add_empty' => true,
//          'query' => $query);
//
//      $this->form->setWidget('employee_id', new sfWidgetFormDoctrineChoice($options));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PositionForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($position = Doctrine::getTable('Position')->find(array($request->getParameter('id'))), sprintf('Object position does not exist (%s).', $request->getParameter('id')));
    $this->form = new PositionForm($position);
    $this->missingShifts = Doctrine::getTable('Shift')->getMissingShifts($position);
    //$this->logMessage('missingShifts: ' . count($this->missingShifts), 'debug');

  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($position = Doctrine::getTable('Position')->find(array($request->getParameter('id'))), sprintf('Object position does not exist (%s).', $request->getParameter('id')));
    $this->form = new PositionForm($position);

    $this->processForm($request, $this->form);
    
    $this->setTemplate('edit');
  }
  public function executeClone(sfWebRequest $request){
    //$this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->forward404Unless($position = Doctrine::getTable('Position')->find(array($request->getParameter('id'))), sprintf('Object position does not exist (%s).', $request->getParameter('id')));
    $cposition = $position->cloneObject();
//    $cposition = new Position();
//
//    $cposition->setLocationId($position->getLocationId());
//    $cposition->setPositionLocation($position->getPositionLocation());
//    $cposition->setPaymentPeriodicity($position->getPaymentPeriodicity());
//    $cposition->setPaymentPeriodicityId($position->getPaymentPeriodicityId());
//    $cposition->setPaymentCalculationType($position->getPaymentCalculationType());
//    $cposition->setShift($position->getShift());
//    $cposition->setName($position->getName());
//    $cposition->setSalary($position->getSalary());
    //$cposition->set
    //$this->form = new PositionForm($position);

    $this->form = new PositionForm($cposition);

    //$this->processForm($request, $this->form);

    $this->setTemplate('new');
      
  }
  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($position = Doctrine::getTable('Position')->find(array($request->getParameter('id'))), sprintf('Object position does not exist (%s).', $request->getParameter('id')));
    $position->delete();

    $this->redirect('position/index');
  }
  public function executeHourlySalaryUpdate(sfWebRequest $request)
  {
    $hourlySalary = $request->getParameter('monthlySalary') / 4.33/ 48;
    //return $this->renderText("96658");
    return $this->renderText($hourlySalary);
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $position = $form->save();
       $this->getUser()->setFlash('notice', 'The item was updated successfully.');
      $this->redirect('position/edit?id='.$position->getId());
    }
  }
}
